package cn.edu.xaut.servlet;

import cn.edu.xaut.Dao.UsersDao;
import cn.edu.xaut.bean.RegisterFormBean;
import cn.edu.xaut.bean.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;

@WebServlet(name = "RegisterServlet", urlPatterns = "/registerServlet")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码
        resp.setContentType( "text/html;charset=utf-8" );
        resp.setCharacterEncoding( "utf-8" );
        req.setCharacterEncoding( "utf-8" );

        //获取表单数据
        String username=req.getParameter( "username" );
        String password=req.getParameter( "password" );
        String password2=req.getParameter( "password2" );
        String email=req.getParameter( "email" );

        //封装数据到RegisterFormBean，用来校验注册是否正确
        RegisterFormBean registerFormBean=new RegisterFormBean();
        registerFormBean.setName( username );
        registerFormBean.setPassword( password );
        registerFormBean.setPassword2( password2 );
        registerFormBean.setEmail( email );

        //校验数据
        if(!registerFormBean.validate()){
            req.setAttribute( "formBean",registerFormBean );
            req.getRequestDispatcher( "/register.jsp" ).forward( req,resp );
            return;
        }

        //如果参数符合要求，封装到User中
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
        user.setRegisterdate(new Date(System.currentTimeMillis()));

        //输出表单数据
        System.out.println(user);

        //数据库操作,插入数据用Dao完成
        UsersDao usersDao = new UsersDao();
        try {
            if (usersDao.findbyname(username) != null) {
                //用户名已存在
                req.setAttribute("DBMessage", "用户已存在注册失败");
                req.setAttribute("formBean", registerFormBean);
                req.getRequestDispatcher("/register.jsp").forward(req, resp);
                return;
            }
            else {
                boolean isSuccessful = usersDao.insert(user);
                if (isSuccessful) {
                    //注册成功
                    resp.getWriter().println("注册成功,3 秒自动跳转");
                    req.getSession().setAttribute("user", user);
                    resp.setHeader("refresh", "3;url=uer_infromation.jsp");
                } else {
                    //注册失败
                    resp.getWriter().write("注册失败");
                }
            }
            }catch(SQLException e ){
                throw new RuntimeException(e);
            }catch(ClassNotFoundException e){
                throw new RuntimeException(e);
            }
    }
}

